﻿using PhuLeBussinessObject.Base;
using PhuLeBussinessObject.Core;
using PhuLeDao.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;

namespace PhuLeBooze.Global
{
    public class CCore
    {
        
        public string InputValueExtend(string inputValue)
        {
            string extend = string.Format("<InputValue UserID='{0}' SecrectToken='{1}' ", CSecurity.USER_ID, CSecurity.SECRECT_TOKEN);
            inputValue = inputValue.Replace("<InputValue", extend);
            return inputValue;
        }
        /// <summary>
        /// Get InputValue from QueryString
        /// </summary>
        /// <param></param>
        /// <returns>InputValue UserID='1' ...</returns>
        public string GetInputValue(HttpContext context = null)
        {
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("InputValue");
            doc.AppendChild(root);
            root.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            root.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());
            string Action = "";
            if (context.Request["Action"] != null)
                Action = context.Request["Action"].ToString();
            if (Action.Equals("GetAdminList"))
            {
                root.SetAttribute("ID", CSecurity.USER_ID.ToString());
            }
            if (context != null)
            {
                HttpRequest request = context.Request;
                HttpResponse response = context.Response;
                StringBuilder qsXml = new StringBuilder();
                string[] allKeys = request.Form.AllKeys;
                if (allKeys.Length == 0)
                {
                    allKeys = request.QueryString.AllKeys;
                }
                foreach (string pKey in allKeys)
                {
                    string key = pKey.Trim();
                    string value = request[key].Trim();
                    if (key == "page")
                        key = "PageIndex";
                    if (key == "rows")
                        key = "RowsPerPage";
                    root.SetAttribute(key, value);
                }
            }
            return doc.OuterXml;
        }

        public IList<CListData> GetListData(string functionCode)
        {
            CCoreDao core = new CCoreDao();
            string inputValue = string.Format("<InputValue UserID='{0}' SecrectToken='{1}' FunctionCode='{2}'/>", CSecurity.USER_ID, CSecurity.SECRECT_TOKEN, functionCode);
            return core.GetListData(inputValue);
        }

        public CApplicationMessage UpdateListData(string inputValue)
        {
            return new CCoreDao().ActionListData(inputValue);
        }

        public CGrid.JQGridResults GetListDataGrid(string inputValue, int numberOfRows, int pageIndex)
        {
            CCoreDao cCoreDao = new CCoreDao();
            IList<CListData> list = cCoreDao.GetListData(inputValue);
            int totalRecords = cCoreDao.LastOutputValue.TotalRows;
            int totalPages = cCoreDao.LastOutputValue.TotalPages;
            CGrid.JQGridResults result = new CGrid.JQGridResults();
            if (list != null && list.Count > 0)
            {
                List<CGrid.JQGridRow> rows = new List<CGrid.JQGridRow>();
                foreach (CListData item in list)
                {
                    CGrid.JQGridRow row = new CGrid.JQGridRow();
                    row.id = item.ID;
                    row.cell = new string[7];
                    row.cell[0] = item.ID.ToString();
                    row.cell[1] = item.Code;
                    row.cell[2] = item.Name;
                    row.cell[3] = item.Description;
                    row.cell[4] = item.ZOrder.ToString();
                    row.cell[5] = item.MinValue.ToString();
                    row.cell[6] = item.MaxValue.ToString();
                    rows.Add(row);
                }
                result.rows = rows.ToArray();
                result.page = pageIndex;
                result.total = totalPages;
                result.records = numberOfRows;
            }
            return result;
        }

    }
}